<?php

class sfWidgetFormWysightml5 extends sfWidgetFormTextarea
{
    /**
     * Configures the current widget.
     *
     * @param array $options     An array of options
     * @param array $attributes  An array of default HTML attributes
     *
     * @see sfWidgetForm
     */
    protected function configure($options = array(), $attributes = array())
    {
      $this->setAttribute('rows', 8);
      $this->setAttribute('cols', 30);
      $this->setAttribute('class', 'wysihtml5');
      $this->addOption('html5', true);
    }

    /**
     * Renders the widget.
     *
     * @param  string $name        The element name
     * @param  string $value       The value displayed in this widget
     * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
     * @param  array  $errors      An array of errors for the field
     *
     * @return string An HTML tag string
     *
     * @see sfWidgetForm
     */
    public function render($name, $value = null, $attributes = array(), $errors = array())
    {
      if($this->getOption('html5'))
          $attributes['class'] = 'wysihtml5';
      else
          $attributes['class'] = 'wysiwyg';

      return parent::render($name, $value, $attributes, $errors);
    }

    private function getJs()
    {
        if($this->getOption('html5'))
            return '';
        else
            return '<script>
                $(document).ready(function() {

                        $(".wysiwyg").wysiwyg({
                            controls: {
                        bold          : { visible : true },
                        italic        : { visible : true },
                        underline     : { visible : true },
                        strikeThrough : { visible : true },

                        justifyLeft   : { visible : true },
                        justifyCenter : { visible : true },
                        justifyRight  : { visible : true },
                        justifyFull   : { visible : true },

                        indent  : { visible : true },
                        outdent : { visible : true },

                        subscript   : { visible : true },
                        superscript : { visible : true },

                        undo : { visible : true },
                        redo : { visible : true },

                        insertOrderedList    : { visible : true },
                        insertUnorderedList  : { visible : true },
                        insertHorizontalRule : { visible : true },

                        cut   : { visible : true },
                        copy  : { visible : true },
                        paste : { visible : true },

                        html  : { visible : true }
                    },
                            events: {
                        click: function(event) {
                            if ($("#click-inform:checked").length > 0) {
                                event.preventDefault();
                                alert("You have clicked jWysiwyg content!");
                            }
                        }
                    }
                        });

                    });
                </script>';
    }

    public function getJavaScripts()
    {
        return array(
            "/js/plugins/jWYSIWYG/jquery.wysiwyg.js",
            "/js/plugins/wysihtml5/wysihtml5-0.3.0.js",
            "/js/plugins/wysihtml5/bootstrap-wysihtml5.js",
            "/js/plugins/wysihtml5/sfWidgetFormWysihtml5.js"
        );
    }

    public function getStylesheets()
    {
        return array(
            "/css/plugins/jquery.jwysiwyg.css" => '',
            "/css/plugins/bootstrap-wysihtml5.css" => ''
        );
    }
}
